package com.dongqi.business.controller;

import com.dongqi.business.service.ExecuteFlowService;
import com.dongqi.config.RestResult;
import com.dongqi.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;

import javax.servlet.http.HttpServletRequest;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.File;

@RestController
@RequestMapping("/executeFlow")
public class ExecuteFlowController {
    @Autowired
    private ExecuteFlowService executeFlowService;

    @PostMapping("/save")
    public RestResult save(HttpServletRequest request) {
        //解析对方发来的xml数据
        try {
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();
            Document doc = db.parse(request.getInputStream());
            // Serialize the document to a file
            TransformerFactory transformerFactory = TransformerFactory.newInstance();
            Transformer transformer = transformerFactory.newTransformer();
            DOMSource source = new DOMSource(doc);
            File file = new File("D:\\Download\\tmp\\test.xml");
            StreamResult result = new StreamResult(file);
            transformer.transform(source, result);
            System.out.println("Document saved to " + file.getAbsolutePath());
            return RestResult.success();
        } catch (Exception e) {
            e.printStackTrace();
        }
       /* if (map.isEmpty()) {
            return RestResult.error(StatusCode.FORBIDDEN.getCode(), "请检查传入参数是否有误");
        } else {
            String xml = map.get("xml").toString();
            if (StringUtils.isEmpty(xml)) {
                return RestResult.error(StatusCode.FORBIDDEN.getCode(), "请检查传入参数是否有误");
            }
            FileWriter fileWriter = null;
            try {
                fileWriter = new FileWriter("D:\\Download\\tmp\\flow.xml");
                fileWriter.write(xml);
                return RestResult.success();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    fileWriter.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            // TODO: 2023/7/24  解析xml并保存到数据库中
            //Boolean flag=executeFlowService.save();
            //if (flag){
            //    return RestResult.success();
            //}else{
            //    return RestResult.error();
            //}
            // TODO: 2023/7/24  测试执行算法流程是否可行

        }*/
        return RestResult.error();
    }

    public static String getValueByTagName(Document doc, String tagName) {
        if (doc == null || StringUtils.isEmpty(tagName)) {
            return "";
        }
        NodeList pl = doc.getElementsByTagName(tagName);
        if (pl != null && pl.getLength() > 0) {
            return pl.item(0).getTextContent();
        }
        return "";
    }
}
